package com.meshnetics.gb.stack.mac.primitives;

import com.meshnetics.gb.stack.core.event.StackEventType;
import com.meshnetics.gb.stack.core.event.StackRequest;
import com.meshnetics.gb.stack.mac.RadioMACStackLayer;
import com.meshnetics.gb.type.impl.zcl.Bool;
import com.meshnetics.gb.type.impl.zcl.TypeBool;
import com.meshnetics.gb.type.impl.zcl.TypeUInt;
import com.meshnetics.gb.type.impl.zcl.UInt;

import java.nio.ByteBuffer;

/**
 * WRITE DESCRIPTION HERE
 *
 * @author AKovylin
 */
public class MACRxEnableRequest extends StackRequest implements MACCommand, MACReplyAwaited {
    public static final byte CODE = 0x56;

    private final Bool deferPermit;
    private final UInt rxOnTime;
    private final UInt rxOnDuration;

    public MACRxEnableRequest(boolean deferPermit, int rxOnTime, int rxOnDuration) {
        this(Boolean.valueOf(deferPermit), new Integer(rxOnTime), new Integer(rxOnDuration));
    }

    public MACRxEnableRequest(Boolean deferPermit, Integer rxOnTime, Integer rxOnDuration) {
        this((Bool) TypeBool.BOOLEAN.fromObject(deferPermit),
                (UInt) TypeUInt.UINT_24.fromObject(rxOnTime),
                (UInt) TypeUInt.UINT_24.fromObject(rxOnDuration));
    }

    public MACRxEnableRequest(Bool deferPermit, UInt rxOnTime, UInt rxOnDuration) {
        super("macRxEnableRequest", StackEventType.LME_TYPE);
        if (deferPermit == null) {
            throw new NullPointerException("deferPermit is null");
        }
        if (rxOnTime == null) {
            throw new NullPointerException("rxOnTime is null");
        }
        if (rxOnDuration == null) {
            throw new NullPointerException("rxOnDuration is null");
        }
        if (deferPermit.isInvalid()) {
            throw new IllegalArgumentException("invalid deferPermit");
        }
        this.deferPermit = deferPermit;
        this.rxOnTime = rxOnTime.convert(TypeUInt.UINT_24);
        this.rxOnDuration = rxOnDuration.convert(TypeUInt.UINT_24);
    }

    public Bool getDeferPermitType() {
        return deferPermit;
    }

    public UInt getRxOnTimeType() {
        return rxOnTime;
    }

    public UInt getRxOnDurationType() {
        return rxOnDuration;
    }

    public Boolean getDeferPermit() {
        return (Boolean) deferPermit.toObject();
    }

    public Integer getRxOnTime() {
        return (Integer) rxOnTime.toObject();
    }

    public Integer getRxOnDuration() {
        return (Integer) rxOnDuration.toObject();
    }

    public byte getCode() {
        return CODE;
    }

    public int getSize(RadioMACStackLayer layer) {
        return 1 + 3 + 3;
    }

    public void serialize(ByteBuffer bb, RadioMACStackLayer layer) {
        deferPermit.serialize(bb);
        rxOnTime.serialize(bb);
        rxOnDuration.serialize(bb);
    }

    public String toString() {
        return getName() + '(' + deferPermit + ',' + rxOnTime + ',' + rxOnDuration + ')';
    }
}
